package my.algorithms;

public class BinarySearch
{
	/**
	 * searches for a value in sorted array
	 *
	 * @param array
	 *            array to search in
	 * @param value
	 *            searched value
	 * @param left
	 *            index of left boundary
	 * @param right
	 *            index of right boundary
	 * @return position of searched value, if it presents in the array or -1, if
	 *         it is absent
	 */
	int binarySearch(int[] array, int value, int left, int right) {
	      if (left > right)
	      {
	            return -1;
	      }
	      
	      // Susceptible to a buffer overflow
	      int middle = (left + right) / 2;
	      
	      if (array[middle] == value)
	            return middle;
	      else if (array[middle] > value)
	            return binarySearch(array, value, left, middle - 1);
	      else
	            return binarySearch(array, value, middle + 1, right);      
	      
	     
	}

}
